//
//  MoreThanVedioViewController.m
//  嘀嘀拍
//
//  Created by 陈一航 on 16/3/2.
//  Copyright © 2016年 wangjia_01. All rights reserved.
//

#import "MoreThanVedioViewController.h"
#import "MoreThanCell.h"
#import "MoreThanDetailViewController.h"
#import "UIScrollView+MJRefresh.h"
static NSString *staticCell=@"MoreThanCell";
@interface MoreThanVedioViewController ()<UICollectionViewDataSource,
UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    NSInteger pageNum;

}
@property (weak, nonatomic) IBOutlet UILabel *currTitle;

@property (weak, nonatomic) IBOutlet UICollectionView *mCollection;
@property (nonatomic,strong)NSMutableArray *dataArray;


@end

@implementation MoreThanVedioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _dataArray=@[].mutableCopy;
    _mCollection.delegate=self;
    _mCollection.dataSource=self;
    _currTitle.text=self.title;
    [_mCollection registerNib:[UINib nibWithNibName:@"MoreThanCell" bundle:nil] forCellWithReuseIdentifier:staticCell];
   
    MJRefreshNormalHeader *refreshHeader=[MJRefreshNormalHeader  headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];

    
    [refreshHeader beginRefreshing];
    [_mCollection setMj_header:refreshHeader];
    
    
    MJRefreshBackNormalFooter *refreshFooter=[MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];

    [_mCollection setMj_footer:refreshFooter];
    
    pageNum=1;

    
}



- (void)headerRefresh
{
    pageNum=1;
    [self getDataSource:YES];

}

- (void)footerRefresh
{
    pageNum++;
    [self getDataSource:NO];
    
}

- (void)getDataSource:(BOOL)isFirst
{   WC;
     [httpRequest getMoreThanVideo:self.categoryID :[NSString stringWithFormat:@"%ld",pageNum] Complement:^(NSDictionary *dic) {
         DLog(@"%@",dic[@"message"]);
         
         if ([dic[@"code"] integerValue]==200) {
             if (isFirst) {
                 [weakSelf.dataArray removeAllObjects];
                 
             }

                 NSArray *array=dic[@"data"];
                 if (![array isEqual:[NSNull null]]) {
                     for (int i=0; i<array.count; i++) {
                         [weakSelf.dataArray addObject:array[i]];
                     }

                 }else
                 {
                     [SVProgressHUD showInfoWithStatus:@"暂无数据"];}

         }else
         {
             [SVProgressHUD showInfoWithStatus:dic[@"message"]];
             
         }
         [weakSelf.mCollection reloadData];
         [weakSelf.mCollection.mj_header endRefreshing];
         [weakSelf.mCollection.mj_footer endRefreshing];
     }];
    
}

#pragma mark 表的代理和数据源

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((__MainScreen_Width-20)/2,__MainScreen_Width/2 );

}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{

    return UIEdgeInsetsMake(5, 5, 5, 5);

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    MoreThanCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:staticCell forIndexPath:indexPath];
    if (self.dataArray.count>0) {
        cell.userName.text=[isLogin isNull:self.dataArray[indexPath.row][@"published_username"]];
        cell.pointZanBtn.tag=indexPath.row;
        [cell.pointZanBtn addTarget:self action:@selector(dianZan:) forControlEvents:UIControlEventTouchUpInside];
        [isLogin setVendorImageUrl:cell.userPhoto :[isLogin isHttpHeader:[isLogin isNull:self.dataArray[indexPath.row][@"avatar"]]]];
        
        [isLogin setVendorImageUrl:cell.bgImg :[isLogin isHttpHeader:[isLogin isNull:self.dataArray[indexPath.row][@"attach"][@"attachment"]]]];
        
        if ([[isLogin isNull:self.dataArray[indexPath.row][@"has_thanks"]] integerValue]>0) {
            cell.pointZanBtn.selected=YES;
        }else
        {
            cell.pointZanBtn.selected=NO;
        }
        
    }
    return cell;

}
- (void)dianZan:(UIButton *)button
{
    WC;
    [httpRequest dianZanISSuccess:self.dataArray[button.tag][@"question_id"] Complement:^(NSDictionary *dic) {
       
        DLog(@"%@",dic);
        [weakSelf getDataSource:YES];
        
    }];
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MoreThanDetailViewController *detailVC=[MoreThanDetailViewController new];
    detailVC.questionID=self.dataArray[indexPath.row][@"question_id"];
    //要传值的
    [self.navigationController pushViewController:detailVC animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)goToPopVC:(id)sender {
    
    [self.navigationController pushViewController:[UIViewController getClass:@"PersonPopViewController"] animated:YES];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
